package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.dao.IProductDao;
import com.financing.wap.dao.IProjectCompanyDao;
import com.financing.wap.dto.ProductAnnualRevenueDTO;
import com.financing.wap.dto.ProductDTO;
import com.financing.wap.dto.ProjectCommentsDTO;
import com.financing.wap.dto.ProjectCompanyDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IProjectCompanyService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.PropertiesUtils;

@Service("productCompanyServiceImpl")
public class ProjectCompanyServiceImpl implements IProjectCompanyService {

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IProjectCompanyDao projectCompanyDaoImpl;

	@Autowired
	private IProductDao productDao;

	@Override
	public List<ProjectCompanyDTO> getProjectCompanyList(
			ProjectCompanyDTO projectCompanyDTO) throws SQLException {
		return projectCompanyDaoImpl.getProjectCompanyList(projectCompanyDTO);
	}

	@Override
	public ProjectCompanyDTO getProjectCompany(Integer id) throws SQLException {
		return projectCompanyDaoImpl.getProjectCompany(id);
	}

	public List<ProductDTO> getCompanyProductList(
			Map<String, Object> queryParams) throws SQLException {

		List<ProductDTO> list = projectCompanyDaoImpl
				.getCompanyProductList(queryParams);

		UserDTO user = (UserDTO) queryParams.get("user");

		for (int i = 0; i < list.size(); i++) {
			ProductDTO product = list.get(i);
			Integer productId = product.getId();

			ProductAnnualRevenueDTO profit = productDao.queryAnnualRevenue(
					productId, "l");
			ProductAnnualRevenueDTO profitHight = productDao
					.queryAnnualRevenue(productId, "h");

			ProductAnnualRevenueDTO rebateProfit = productDao
					.queryAnnualRevenue(productId, "rl");
			ProductAnnualRevenueDTO rebateProfitHight = productDao
					.queryAnnualRevenue(productId, "rh");
			if (profit == null) {
				continue;
			}
			product.setProfit(profit);
			product.setProfitHight(profitHight);

			profit.setRebate(rebateProfit.getRebate());
			profitHight.setRebate(rebateProfitHight.getRebate());

			if ((user == null || user.getId() == null||"1".equals(user.getRole()) || "99".equals(user.getRole()) || "98".equals(user.getRole())) && profit != null) {
				profit.setRebate(null);
				profitHight.setRebate(null);
			}
			if (Constants.FP_CORP_TYPE_PRIVATE.equals(product
					.getProjectCorpType())) {
				// 内销产品 佣金字段为null;
				profit.setRebate(null);
				profitHight.setRebate(null);
			}

			// 平台销售人数及额度。
			Map<String, Object> map = productDao
					.queryProductBuyNumberAndAmount(productId);

			/*************************** 所有销售额及销售个数的计算 ****************************/
			BigDecimal sumAmount = (BigDecimal) map.get("sumAmount");
			Long num = (Long) map.get("num");
			logger.info("==>project num:" + num);
			logger.info("==>project sumAmount:" + sumAmount);

			if (sumAmount == null) {
				sumAmount = new BigDecimal(0);
			}
			if (num == null) {
				num = new Long(0);
			}
			logger.info("==>project allSubscriptionNum:"
					+ product.getAllSubscriptionNum());
			logger.info("==>project allSubscriptionAmount:"
					+ product.getAllSubscriptionAmount());

			product.setAllSubscriptionNum(num.intValue()
					+ product.getAllSubscriptionNum());
			product.setAllSubscriptionAmount(sumAmount.add(product
					.getAllSubscriptionAmount()));
			/*************************** 所有销售额及销售个数的计算END ****************************/

			product.setAppointmentAmount(sumAmount);
			product.setAppointmentNumber(num);
			product.setComment(null);
			/** 产品图片处理 **/
			String pImagePath = product.getpImageUrl();
			if (pImagePath != null) {
				product.setpImageUrl(PropertiesUtils.getValue("ctxPath")
						+ pImagePath);
			}

		}
		return list;
	}

	public int getCompanyProductListCount(Map<String, Object> queryParams)
			throws SQLException {
		return projectCompanyDaoImpl.getCompanyProductListCount(queryParams);
	}

}
